#include "input.h"
#include <stdio.h>
#include <cstring>

bool IsKeyDown(int vk_code)
{
    return (GetAsyncKeyState(vk_code) & 0x8000);
}

Input::Input()
{
    m_command = CM_STOP;
    zeroStates();
}

Input::~Input()
{


}

void Input::zeroStates()
{
    memset(m_commandState,0,sizeof(m_commandState));
}

void Input::test()
{
    printf("cmd:%d \n",m_command);
}


void Input::update(float detalTime)
{

    zeroStates();
    m_command = CM_STOP;

    if(IsKeyDown(K_LEFT) || IsKeyDown(K_A))
    {
        m_command = CM_LEFT;
        m_commandState[CM_LEFT] = 1;
    }

    if(IsKeyDown(K_RIGHT) || IsKeyDown(K_D))
    {
        m_command = CM_RIGHT;
        m_commandState[CM_RIGHT] = 1;
    }

    if(IsKeyDown(K_UP) || IsKeyDown(K_W) || IsKeyDown(K_LB))
    {
        m_command = CM_UP;
        m_commandState[CM_UP] = 1;
    }

    if(IsKeyDown(K_DOWN) || IsKeyDown(K_S) || IsKeyDown(K_RB))
    {
        m_command = CM_DOWN;
        m_commandState[CM_DOWN] = 1;
    }

    if(IsKeyDown(K_ESC) || IsKeyDown(K_Q))
    {
        m_command = CM_QUIT;
        m_commandState[CM_QUIT] = 1;
    }

    if(m_command == CM_STOP)
    {
        m_commandState[CM_STOP] = 1;
    }
}

bool Input::getCommandState(COMMAND cmd)
{
    return (m_commandState[cmd] == 1);
}
